#ifndef CODIGOSDELTA_H_
#define CODIGOSDELTA_H_

#include "BitWriter.h"
#include "BitReader.h"

void deltaEncode(int source, BitWriter* bitwriter) {
	int num = source;
	int len = 0;
	int lengthOfLen = 0;
	// calcula 1+floor(log2(num))
	for (int temp = num; temp > 0; temp >>= 1) {
		len++;
	}
	// calcula floor(log2(len))
	for (int temp = len; temp > 1; temp >>= 1) {
		lengthOfLen++;
	}
	for (int i = lengthOfLen; i > 0; --i) {
		bitwriter->escribirBit(0);
	}
	for (int i = lengthOfLen; i >= 0; --i) {
		bitwriter->escribirBit((len >> i) & 1);
	}
	for (int i = len - 2; i >= 0; i--) {
		bitwriter->escribirBit((num >> i) & 1);
	}

}

int deltaDecode(BitReader* bitreader) {

	int decoded_delta = 1;
	int len = 1;
	int lengthOfLen = 0;
	while (!bitreader->leerBit()) {
		lengthOfLen++;
	}
	for (int i = 0; i < lengthOfLen; i++) {
		len <<= 1;
		if (bitreader->leerBit())
			len |= 1;
	}
	for (int i = 0; i < len - 1; i++) {
		decoded_delta <<= 1;
		if (bitreader->leerBit())
			decoded_delta |= 1;
	}

	return decoded_delta;
}

#endif /* CODIGOSDELTA_H_ */
